package com.example.distillapplication.ui.activity;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;

import android.content.Intent;
import android.graphics.PointF;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import com.example.distillapplication.R;
import com.example.distillapplication.data.entity.UserInfo;
import com.example.distillapplication.ui.activity.base.BaseActivity;
import com.example.distillapplication.databinding.ActivityHelloBinding;
import com.example.distillapplication.hardware.SerialManager;
import com.example.distillapplication.hardware.protcal.Protocol;
import com.example.distillapplication.view.bean.CircleBean;
import com.example.distillapplication.view.utils.DisplayUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class HelloActivity extends BaseActivity {
    ActivityHelloBinding activityHelloBinding;
    private static final String TAG = "allen";
    private List<CircleBean> circleBeanList = new ArrayList<>();
    Intent intent;
    Handler handler;
    Thread safeCheckThread;

    //开机动画，欢迎界面
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_hello);
        activityHelloBinding = DataBindingUtil.setContentView(this, R.layout.activity_hello);

        initPoint();

        activityHelloBinding.circleView.setmCircleBeen(circleBeanList);

        activityHelloBinding.circleView.setCenterImg(activityHelloBinding.centerTv);
        activityHelloBinding.circleView.openAnimation();

        intent = new Intent(HelloActivity.this, LoginActivity.class);
        handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                startActivity(intent);
                finish();
            }
        }, 6000);

        SerialManager.getInstance().setSerialPort2BaudRate(9600);//设置波特率与初始化

        //串口队列的初始化
        Queue<byte[]> q = new LinkedList<>();
        q.offer(Protocol.selfCheck);
        mSerialMessageViewModel.serialQueue.postValue(q);

        //串口接收缓存的初始化
        byte[] recieveByte = new byte[100];
        mSerialMessageViewModel.serialBuffer.postValue(recieveByte);

        mInstrumentStateViewModel.instrumentSelfTest.postValue(false);

        //开机自检线程
        safeCheckThread = new Thread(new Runnable() {
            @Override
            public void run() {
                byte[] recieveByte = new byte[100];
                try {
                    SerialManager.getInstance().ttyS2Send(Protocol.selfCheck);
                    SerialManager.getInstance().ttyS2Recieve(recieveByte);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                mSerialMessageViewModel.serialBuffer.postValue(recieveByte);
            }
        });
        safeCheckThread.start();

        mSerialMessageViewModel.serialBuffer.observe(this, new Observer<byte[]>() {
            @Override
            public void onChanged(byte[] bytes) {
                if(bytes[1] == 0x01 && bytes[2] == 0x01 && bytes[4] == 0x61){
                    //自检成功
                    mInstrumentStateViewModel.instrumentSelfTest.postValue(true);
                }
            }
        });



        //mDataBaseViewModel.dataBaseRepository.insertUserInfo(new UserInfo("管志强","root","123456","天津大学","天津市南开区"));

    }


    private void initPoint() {

        int height = DisplayUtils.getDisplayHight(this);
        int width = DisplayUtils.getDisplayWidth(this);

        int centerX = width / 2;
        int centerY = height / 2;


        Log.d(TAG, "initPoint: " + centerX + "----" + centerY);


        CircleBean circleBean = new CircleBean(
                new PointF((float) (-width / 5.1), (float) (height / 1.5)),
                new PointF(centerX - 30, height * 2 / 3),
                new PointF((float) (width / 2.4), (float) (height / 3.4)),
                new PointF(width / 6, centerY - 120),
                new PointF((float) (width / 7.2), -height / 128),
                (float) (width / 14.4), 60);
        CircleBean circleBean2 = new CircleBean(
                new PointF(-width / 4, (float) (height / 1.3)),
                new PointF(centerX - 20, height * 3 / 5),
                new PointF((float) (width / 2.1), (float) (height / 2.5)),
                new PointF(width / 3, centerY - 10),
                new PointF(width / 4, (float) (-height / 5.3)),
                width / 4, 60);
        CircleBean circleBean3 = new CircleBean(
                new PointF(-width / 12, (float) (height / 1.1)),
                new PointF(centerX - 100, height * 2 / 3),
                new PointF((float) (width / 3.4), height / 2),
                new PointF(0, centerY + 100),
                new PointF(0, 0),
                width / 24, 60);

        CircleBean circleBean4 = new CircleBean(
                new PointF(-width / 9, (float) (height / 0.9)),
                new PointF(centerX, height * 3 / 4),
                new PointF((float) (width / 2.1), (float) (height / 2.3)),
                new PointF(width / 2, centerY),
                new PointF((float) (width / 1.5), (float) (-height / 5.6)),
                width / 4, 60);

        CircleBean circleBean5 = new CircleBean(
                new PointF((float) (width / 1.4), (float) (height / 0.9)),
                new PointF(centerX, height * 3 / 4),
                new PointF(width / 2, (float) (height / 2.37)),
                new PointF(width * 10 / 13, centerY - 20),
                new PointF(width / 2, (float) (-height / 7.1)),
                width / 4, 60);
        CircleBean circleBean6 = new CircleBean(
                new PointF((float) (width / 0.8), height),
                new PointF(centerX + 20, height * 2 / 3),
                new PointF((float) (width / 1.9), (float) (height / 2.3)),
                new PointF(width * 11 / 14, centerY + 10),

                new PointF((float) (width / 1.1), (float) (-height / 6.4)),
                (float) (width / 4), 60);
        CircleBean circleBean7 = new CircleBean(
                new PointF((float) (width / 0.9), (float) (height / 1.2)),
                new PointF(centerX + 20, height * 4 / 7),
                new PointF((float) (width / 1.6), (float) (height / 1.9)),
                new PointF(width, centerY + 10),

                new PointF(width, 0),
                (float) (width / 9.6), 60);

        circleBeanList.add(circleBean);
        circleBeanList.add(circleBean2);
        circleBeanList.add(circleBean3);
        circleBeanList.add(circleBean4);
        circleBeanList.add(circleBean5);
        circleBeanList.add(circleBean6);
        circleBeanList.add(circleBean7);

    }

    /**
     * 获取状态栏高度
     *
     * @return
     */
    public int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }


    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume: ");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause: ");
    }
}
